import React, { Component } from 'react'
import { connect } from 'dva';
import Link from 'umi/link';
import { Table, Divider, Modal, message } from 'antd';
import Form from './components/SearchForm';
import { checkInit } from '@/utils/util';

export interface Props {
  dispatch?: Function,
  children?: React.ReactNode,
  loading: boolean,
  list: {
    data: Array<any>,
    dataCnt: number
  },
  buttons?: Array<any>,
  dict?: object
}

const ONE_PAGE_SIZE = 10;

@connect(({ unit_task_rules, loading, menu }) => ({
  list: unit_task_rules.list || {},
  dict: unit_task_rules.dict,
  buttons: menu.buttons,
  loading: loading.effects['unit_task_rules/queryPage']
}))
class Position extends Component<Props, any> {
  constructor(props) {
    super(props);
    this.initCurrent = this.initCurrent.bind(this);
  }
  state = {
    current: 1,
    selectedRowKeys: [],
    userIdList: [],
    rowDatas: []
  };

  queryData = (offset = 1, size = ONE_PAGE_SIZE) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'unit_task_rules/queryPage',
      payload: {
        pageNum: offset,
        pageSize: size,
        resType: '1'
      },
    });
  }

  initCurrent() {
    this.setState({
      current: 1
    })
  }

  removeOption = (results) => {
    if(results) {
      this.setState({
        selectedRowKeys: []
      });
    }
  };

  onSelectChange = (selectedRowKeys, rowData) => {
    this.state.userIdList = [];
    this.setState({
      selectedRowKeys,
      rowDatas: rowData
    });
    rowData.forEach(item => {
      this.state.userIdList.push(item.id);
    });
  };

  changeOption = () => {
    this.setState({
      selectedRowKeys: []
    });
  };

  componentDidMount() {
    const { dispatch } = this.props;
    checkInit() ? dispatch({ type: `unit_task_rules/initCond` }) : this.queryData();
  }

  render() {
    const { buttons } = this.props;
    const btnList = {};
    if(Array.isArray(buttons)) {
      buttons.forEach(item => {
        btnList[item.name] = item.name
      });
    }
    const rowSelection = {
      selectedRowKeys: this.state.selectedRowKeys ,
      onChange: this.onSelectChange,
    };
    const { data = [], dataCnt } = this.props.list;
    const ans = data && Array.isArray(data) ? data.map((item, index) => ({
      key: index,
      id: item.id,
      name: item.name,
      taskTypeName: item.taskTypeName ? item.taskTypeName : ' ',
      beginTime: item.beginTime ? item.beginTime : ' ',
      endTime: item.endTime ? item.endTime : ' ',
      statusName: item.statusName ? item.statusName : ' ',
      status: item.status,
      cycle: item.cycle ? item.cycle : ' ',
      auditTypeName: item.auditTypeName ? item.auditTypeName : ' '
      })
    ) : [];
    const pagination = {
      total: dataCnt,
      showTotal: total => `共 ${total} 条`,
      showQuickJumper: true,
      pageSize: ONE_PAGE_SIZE,
      onChange: (current) => {
        this.setState({ current });
        this.queryData(current)
      },
    };
    const columns = [{
        title: '规则名称',
        dataIndex: 'name',
      }, {
        title: '任务类型',
        dataIndex: 'taskTypeName',
      }, {
        title: '开始时间',
        dataIndex: 'beginTime',
      }, {
        title: '截止时间',
        dataIndex: 'endTime',
      }, {
        title: '状态',
        dataIndex: 'statusName',
      }, {
        title: '检查频率（天）',
        dataIndex: 'cycle',
      }, {
        title: '操作', key: 'x', render: (record) => {
        return (
          <div style={{display: 'flex'}}>
            {
              btnList['查看'] ? <>
                <Link to={`/data/unit_task_rules/${record.id}/details`}>查看</Link>
              </> : null
            }
          </div>
        )}
      }];
    return (
      <div>
        <Form
          current={ this.state.current }
          initCurrent={this.initCurrent}
          removeOption={this.removeOption}
          changeOption={this.changeOption}
          userIdList={this.state.userIdList}
          rowDatas={this.state.rowDatas}/>
        <Table
          columns={columns}
          pagination={pagination}
          dataSource={ans}
          bordered
          loading={this.props.loading}
          rowSelection={ rowSelection }
        />
      </div>
    );
  }
}

export default Position;
